<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChRoleScheme;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Model;
use Mine\Abstracts\AbstractMapper;

class ChRoleSchemeMapper extends AbstractMapper
{

    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChRoleScheme::class;
    }

    /**
     * 根据ID获取角色方案信息
     * @param mixed $id
     * @return ChRoleScheme|Model|string|Builder|null
     */
    #[Cacheable(prefix: 'role_scheme_info',value: 'id_#{id}',ttl: 15 * 60)]
    public function findById(mixed $id): ChRoleScheme|Model|string|Builder|null
    {
        return $this->model::query()
            ->where('id',$id)
            ->first();
    }

    public function clearCache(mixed $id): void
    {
        $redis = redis();
        $keys[] = $this->prefix . 'role_scheme_info:id_'.$id;
        $redis->del($keys);
    }

}